package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.activity.find.TopicDetailActivity;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.NewObExchangeBean;
import com.mz.ozy.mvp.presenter.ExchangeObPresenter;
import com.mz.ozy.mvp.view.ExchangeObContract;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.widget.dialog.BaseDialog;
import com.mz.ozy.widget.dialog.PayPassDialog;
import com.mz.ozy.widget.dialog.PayPassView;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 兑换欧花
 */
public class ExchangeObActivity extends BaseActivity<ExchangeObContract.View, ExchangeObPresenter> implements ExchangeObContract.View {

    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.tv_dzAmt)
    TextView tvDzAmt;
    private String balance;

    PayPassDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange_ob);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("兑换欧花");
        balance = getIntent().getStringExtra("balance");
        tvBalance.setText("当前余额：" + balance);
        etNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Utils.editTextUtils(s, etNumber, 3);
                if (TextUtils.isEmpty(s.toString()) || s.toString().startsWith(".") || Double.parseDouble(s.toString()) <= 0) {
                    tvDzAmt.setText("0.00");

                } else {
                    tvDzAmt.setText(s.toString());

                }
            }
        });

    }

    @OnClick(R.id.bt_confirm)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etNumber.getText().toString()) || Double.parseDouble(etNumber.getText().toString()) == 0) {
            ToastUtils.showToast("请输入兑换金额，大于1");
            return;
        }
        if (Double.parseDouble(etNumber.getText().toString()) > Double.parseDouble(balance)) {
            ToastUtils.showToast("余额不足");
            return;
        }

        String amount = etNumber.getText().toString();

//        final BaseDialog dialog = new BaseDialog(ExchangeObActivity.this, R.layout.dialog_private, Gravity.CENTER, true);
//        ((TextView) dialog.getView(R.id.tv_dialog_content)).setText("确定兑换"+ amount +"欧花?");
//        ((TextView) dialog.getView(R.id.tv_dialog_title)).setText("提示");
//        ((TextView) dialog.getView(R.id.btn_confirm)).setText("确定");
//        dialog.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.toggleDialog();
//            }
//        });
//        dialog.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mPresenter.newOnExchange(ExchangeObActivity.this, amount);
//                dialog.toggleDialog();
//            }
//        });
//        dialog.toggleDialog();

        dialog = new PayPassDialog(this);
        dialog.getPayViewPass().setForgetText(amount);
        dialog.getPayViewPass().setHintText("确认兑换欧花");
        dialog.getPayViewPass()
                .setPayClickListener(new PayPassView.OnPayClickListener() {
                    @Override
                    public void onPassFinish(String passContent) {
                        //6位输入完成回调
                        dialog.dismiss();
                        mPresenter.newOnExchange(ExchangeObActivity.this, amount, MD5Util.getMD5String(passContent));
                    }

                    @Override
                    public void onPayClose() {
                        dialog.dismiss();
                        //关闭回调
                    }

                    @Override
                    public void onPayForget() {
                        dialog.dismiss();
                        //点击忘记密码回调
                        startActivity(new Intent(ExchangeObActivity.this, PayPwdActivity.class));
                    }
                });
    }

    @Override
    public void onExchangeSuccess(JSONObject json) {
        dialog.dismiss();

        ToastUtils.showToast("兑换成功");
        finish();
    }

    @Override
    public void newOnExchangeSuccess(NewObExchangeBean obExchangeBean) {
        ToastUtils.showToast(obExchangeBean.msg);
        finish();
    }
}